package UMC.Web;

import UMC.Data.Utility;

import java.net.URI;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class UIFormDialog extends UIDialog {
    private List<WebMeta> _dataSource = new LinkedList<>();

    @Override
    protected String type() {
        return "Form";
    }

    private WebMeta meta(String type, String name, String title) {
        WebMeta v = new WebMeta();
        v.put("Type", type).put("Name", name)
                .put("Title", title);
        this._dataSource.add(v);
        return v;
    }

    public void addHeader(UIHeader header) {
        this.config.put("Header", header);
    }

    public void addFooter(UIHeader header) {
        this.config.put("Footer", header);
    }


    public List<WebMeta> getDataSource() {
        return _dataSource;
    }

    private WebMeta meta(String type, String name, String title, String defaultValue) {

        return meta(type, name, title).put("DefaultValue", defaultValue);
    }

    public void menu(String text, String model, String cmd, String value) {
        this.menu(new UIClick(model, cmd, value).text(text));
    }

    public void menu(UIClick... menus) {
        this.config.put("menu", menus);
    }

    public void menu(String text, String model, String cmd, WebMeta param) {
        this.menu(new UIClick(model, cmd, param).text(text));
    }

    public WebMeta add(String type, WebMeta value, WebMeta format, UIStyle style) {
        WebMeta v = new WebMeta().put("Type", type).put("value", value).put("format", format).put("style", style);
        this._dataSource.add(v);
        return v;
    }

    public WebMeta add(UICell cell) {
        WebMeta v = new WebMeta().put("Type", cell.type()).put("value", cell.data()).put("format", cell.format())
                .put("style", cell.style());
        this._dataSource.add(v);
        return v;

    }

    public void addSlider(String title, String Code, int defaultValue, int min, int max) {

        meta("FieldSlider", Code, title).put("Max", max).put("Min", min).put("DefaultValue", defaultValue);
    }

    public void addSlider(String title, String Code, int defaultValue) {
        addSlider(title, Code, defaultValue, 0, 100);
    }

    public void addAddress(String title, String Code, String defaultValue) {

        meta("Address", Code, title).put("DefaultValue", defaultValue);
    }

    public void addScore(String title, String Code, int defaultValue) {

        meta("Score", Code, title).put("DefaultValue", defaultValue);
    }


    public WebMeta addNumber(String title, String Code, Integer defaultValue) {

        return meta("Number", Code, title, defaultValue + "");// .put("Vtype", "Phone");
    }

    public WebMeta addNumber(String title, String Code, String defaultValue) {

        return meta("Number", Code, title, defaultValue);// .put("Vtype", "Phone");
    }


    public WebMeta add(String type, String Code, String title, String defaultValue) {

        return meta(type, Code, title, defaultValue);

    }

    public void addNumber(String title, String Code, Float defaultValue) {
        meta("Number", Code, title, defaultValue + "");
    }

    public void addFooter(String caption, String name, String defaultValue) {
        meta("Confirm", name, null, defaultValue).put("Text", caption);
    }

    public void addFooter(String caption, boolean isForm) {
        if (isForm) {
            this.addFooter(caption, "_FOOTER_NAME", "YES");
        } else {
            this.addFooter(caption);
        }
    }

    public void addFooter(String format, WebMeta v, UIStyle style) {
        v.put("Type", "Footer").put("Format", format).put("Style", style);
        this._dataSource.add(v);
    }

    public void addFooter(String text) {
        WebMeta v = new WebMeta();
        v.put("Type", "Footer")
                .put("Text", text);
        this._dataSource.add(v);
    }

    public WebMeta addScan(String title, String Code, String defaultValue) {

        return meta("Scan", Code, title, defaultValue);
    }

    public WebMeta addOption(String title, String code, String value, String text) {
        return meta("Option", code, title, value).put("Text", text);
    }

    public WebMeta addColor(String title, String code, String value) {
        return meta("Option", code, title, value).put("Color", true).command("System", "Color");
    }

    public WebMeta addIcon(String title, String code, String value) {
        return meta("Option", code, title, value).put("Icon", true).command("System", "Icon");
    }

    public WebMeta addRadioText(String name, String text) {
        WebMeta v = new WebMeta();
        v.put("Type", "RadioText").put("Name", name).put("Text", text);

        this._dataSource.add(v);
        return v;
    }

    public WebMeta addRadioText(String name, String text, String marker) {
        WebMeta v = new WebMeta();
        v.put("Type", "RadioText").put("Name", name).put("Text", text).put("Marker", marker);

        this._dataSource.add(v);
        return v;
    }

    public WebMeta addFile(String title, String Code, String defaultValue) {

        return meta("File", Code, title, defaultValue);// .put("Text", text);
    }

    public WebMeta addFiles(String title, String Code) {
        return meta("Files", Code, title);// defaultValue);/
    }

    public WebMeta addTextarea(String title, String Code, String defaultValue) {

        return meta("Textarea", Code, title, defaultValue);
    }

    public WebMeta addDate(String title, String code, Date date) {

        WebMeta v = meta("Date", code, title);
        if (date != null) {
            java.text.DateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
            v.put("DefaultValue", format.format(date));
        }
        return v;
    }


    public WebMeta addText(String title, String code, String defaultValue) {

        return meta("Text", code, title, defaultValue);
    }

    public WebMeta addTextValue(String title, ListItemCollection items) {
        WebMeta v = new WebMeta();
        if (!Utility.isEmpty(title))
            v.put("Title", title);

        v.put("DataSource", items);
        v.put("Type", "TextValue");

        this._dataSource.add(v);
        return v;
    }

    public WebMeta addTextValue(ListItemCollection items) {
        return this.addTextValue("", items);
    }

    public WebMeta addTextNameValue(ListItemCollection items) {
        return this.addTextNameValue("", items);
    }

    public WebMeta addTextNameValue(String title, ListItemCollection items) {

        WebMeta v = new WebMeta();
        if (Utility.isEmpty(title) == false) {
            v.put("Title", title);
        }

        v.put("DataSource", items);
        v.put("Type", "TextNameValue");

        this._dataSource.add(v);
        return v;
    }

    public ListItemCollection addTextNameValue(String title) {
        ListItemCollection t = new ListItemCollection();
        this.addTextNameValue(title, t);
        return t;
    }

    public ListItemCollection addTextNameValue() {
        ListItemCollection t = new ListItemCollection();
        this.addTextNameValue(t);
        return t;
    }

    public ListItemCollection addTextValue(String title) {
        ListItemCollection t = new ListItemCollection();
        this.addTextValue(title, t);
        return t;
    }

    public ListItemCollection addTextValue() {
        ListItemCollection t = new ListItemCollection();
        this.addTextValue(t);
        return t;
    }

    public void addImage(URI src) {
        WebMeta v = new WebMeta();
        v.put("Type", "Image");
        v.put("Src", src.toString());
        v.put("Name", "_image_" + this._dataSource.size());

        this._dataSource.add(v);
    }

    public WebMeta addPassword(String title, String Code, boolean isEnableMd5) {
        WebMeta v = meta("Password", Code, title);

        if (isEnableMd5) {
            v.put("isEnableMd5", true);

        }
        return v;
    }

   
    public WebMeta addTime(String title, String code, int hour, int minute) {
        WebMeta v = meta("Time", code, title, hour + ":" + minute);
        return v;

    }

    public WebMeta addTime(String title, String code, Date defaultValue) {
        WebMeta v = meta("Time", code, title);
        if (defaultValue != null) {
            java.text.DateFormat format = new java.text.SimpleDateFormat("HH:mm");
            v.put("DefaultValue", format.format(defaultValue));
        }
        return v;
    }

    public ListItemCollection addSelect(String title, String code) {
        ListItemCollection t = new ListItemCollection();
        addSelect(title, code, t);
        return t;
    }

    public WebMeta addSelect(String title, String code, ListItemCollection items) {
        WebMeta v = meta("Select", code, title);

        v.put("DataSource", items);
        this._dataSource.add(v);
        return v;
    }

    public ListItemCollection addCheckBox(String title, String code, String defaultValue) {
        ListItemCollection t = new ListItemCollection();
        addCheckBox(title, code, t, defaultValue);
        return t;
    }

    public ListItemCollection addCheckBox(String title, String code) {
        ListItemCollection t = new ListItemCollection();
        addCheckBox(title, code, t);
        return t;
    }

    public WebMeta addCheckBox(String title, String code, ListItemCollection items, String defaultValue) {

        WebMeta v = meta("CheckboxGroup", code, title, defaultValue);

        v.put("DataSource", items);
        return v;
    }

    public WebMeta addCheckBox(String title, String code, ListItemCollection items) {
        return addCheckBox(title, code, items, null);
    }

    public ListItemCollection addRadio(String title, String code) {
        ListItemCollection t = new ListItemCollection();
        addRadio(title, code, t);
        return t;
    }

    public WebMeta addRadio(String title, String code, ListItemCollection items) {
        WebMeta v = meta("RadioGroup", code, title);
        v.put("DataSource", items);
        return v;
    }

    public void submit(String btnName) {
        submitText(btnName);

        this._dataSource.get(this._dataSource.size() - 1).put("Submit", "YES");
    }

    boolean _isHideSubmit;

    @Override
    protected void initSubmit(WebRequest request) {

        WebMeta p = new WebMeta();
        p.put("send", new WebMeta(request.Arguments).put(KEY_DIALOG_ID, this.asyncId()));
        p.put("model", request.model());
        p.put("cmd", request.cmd());
        if (!Utility.isEmpty(submitText())) {
            p.put("text", submitText());
        }
        if (_isHideSubmit) {
            p.put("hide", true);
        }
        this.config.put("Name", this.asyncId());
        this.config.put("Submit", p);

    }

    public void hideSubmit() {
        _isHideSubmit = true;
    }

    public void submit() {
        this._dataSource.get(this._dataSource.size() - 1).put("Submit", "YES");
    }

    public WebMeta addVerify(String title, String code, String placeholder) {

        WebMeta v = meta("Verify", code, title).put("placeholder", placeholder);
        return v;
    }

    public void submit(String btnName, String... colseEvent) {
        if (colseEvent.length > 0) {
            this.config.put("CloseEvent", String.join(",", colseEvent));
        }
        submit(btnName);
    }

    protected void initialization(WebContext context) {

        this.config.put("DataSource", _dataSource);
    }

}